package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: JW
 * @Description: TODO
 * @DateTime: 2022-02-10 21:03
 **/
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //参数校验
        if (multipartFile == null || multipartFile.getSize() == 0){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        //拿到后缀名
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        //文件名
        String filename = UUID.randomUUID().toString() + "." + suffix;

        //文件全路径
        String url = "";

        //拿到文件流
        try {
            InputStream inputStream = multipartFile.getInputStream();
            url = fileStorageService.uploadImgFile("", filename, inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //保存数据
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setUrl(url);
        wmMaterial.setCreatedTime(new Date());

        save(wmMaterial);

        return ResponseResult.okResult("上传成功");
    }

    @Override
    public ResponseResult getList(WmMaterialDto wmMaterialDto) {

        if (ObjectUtils.isEmpty(wmMaterialDto)){
            return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.PARAM_INVALID);
        }

        //分页条件
        Page<WmMaterial> wmMaterialPage = new Page<>(wmMaterialDto.getPage(),wmMaterialDto.getSize());

        //查询条件
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (wmMaterialDto.getIsCollection() == 1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }

        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //分页查询
        page(wmMaterialPage, lambdaQueryWrapper);


        ResponseResult responseResult = new PageResponseResult(
                wmMaterialDto.getPage(),wmMaterialDto.getSize(), (int) wmMaterialPage.getTotal());
        //responseResult.setData(wmMaterialPage.getRecords());
        return responseResult.ok(wmMaterialPage.getRecords());
    }
}
